#Test script of Org Smoke Test
#require File.dirname(__FILE__)+'/../lib/testdata.rb'
require File.dirname(__FILE__)+'/../lib/elements/Sales/element_OrgSearch.rb'
require File.dirname(__FILE__)+'/../lib/elements/element_Org.rb'
require File.dirname(__FILE__)+'/../lib/reporting.rb'
require File.dirname(__FILE__)+'/../lib/std_lib.rb'
require 'watir'
require 'watir/ie'
require 'watir/contrib/enabled_popup'
require 'watir/testcase'

class Smoke_Test_Org < Watir::TestCase
  
    # open INT or QA
    CRM_STD_LIB.login($qa,"tony2008","2165270")

    def test_setup
        filename  = File.basename(__FILE__).gsub(/\.rb/, "")

        begin
            $log=Reporting.new(File.dirname(__FILE__)+'/../doc/report/'"",filename)
            CRM_STD_LIB.open_page("Org Search")
            #$ie = Watir::IE.attach(:title, "Tonytest_20090909_update")
            @@orgname = "Tonytest_"+Time.now.strftime("%Y%m%d")
            @@orgname1=@@orgname + "_update"
            $ie.bring_to_front
            #            if $ie = Watir::IE.find(:title,"ActiveCRM")
            #                $ie = Watir::IE.start("https://crmint.dev.activenetwork.com/admin/orgcrm/segmentation/org_market_data_search.cfm")
            #            else
            #                $ie = Watir::IE.start("https://crmint.dev.activenetwork.com/admin/orgcrm/segmentation/org_market_data_search.cfm")
            #            end
        rescue
            puts $!
            puts $@
            $log.close unless $log.nil?
        end
    end

    # Create New Org without Product Family
    def test_create_new_org_without_product_family
        #$log.test_notes("OrgSmokeTest-INT")
        $log.test_notes("OrgSmokeTest-QA")
        $log.test_notes("Start to test Create New Org without Product Family")

        Element::OrgSearch.new_org_button.click
        sleep 1
        $ie=Watir::IE.attach(:title,"0")
        Element::CreateNewOrg.country_select_list.select("United States (USD)")
        Element::CreateNewOrg.next_button.click
        sleep 1
        Element::CreateNewOrg.org_name_text_field.set(@@orgname)
        Element::CreateNewOrg.state_select_list.select("AA")
        #        Element::CreateNewOrg.org_url_text_field.set("www.test.com")
        #        Element::CreateNewOrg.address1_text_field.set("Keji 2 Road")
        #        Element::CreateNewOrg.address2_text_field.set("QinFengGe D201")
        #        Element::CreateNewOrg.city_ text_field.set("Xi'an")
        #        Element::CreateNewOrg.zip_text_field.set("710075")
        #        Element::CreateNewOrg.phone_text_field.set("1234567890")
        #        Element::CreateNewOrg.fax_text_field.set("0123456789")
        #        Element::CreateNewOrg.email_text_field.set("tonytest@test.com")
        #        Element::CreateNewOrg.participants_text_field.set("1000")
        #        Element::CreateNewOrg.census_pop_text_field.set("10000")
        #        Element::CreateNewOrg.no_of_members_text_field.set("1000")
        Element::CreateNewOrg.next_button.click
        sleep 1
        Element::CreateNewOrg.external_classifier_select_list.select("For-Profit")
        Element::CreateNewOrg.market1_select_list.select("Events")
        Element::CreateNewOrg.submarket1_select_list.select("Business")
        Element::CreateNewOrg.type1_select_list.select("Enterprise Corporation")
        Element::CreateNewOrg.create_button.click
        $log.test_validate(Element::EditOrg.orgname_text_field.value.include?(@@orgname), "Org Name correct or not")
        $log.test_validate($ie.text.include?("AA"),  "State Name correct or not")
        $log.test_validate($ie.text.include?("United States (USD)"),  "Country Name correct or not")
        sleep 10

    end
    # Create New Org with Product Family
    def test_create_new_org_with_product_family
        $log.test_notes("Start to test Create New Org with Product Family")
        CRM_STD_LIB.open_page("Org Search")
        Element::OrgSearch.new_org_button.click
        sleep 1
        $ie=Watir::IE.attach(:title,"0")
        Element::CreateNewOrg.country_select_list.select("Canada (CAD)")
        Element::CreateNewOrg.next_button.click
        sleep 1
        Element::CreateNewOrg.org_name_text_field.set(@@orgname)
        Element::CreateNewOrg.state_select_list.select("AB")
        #        Element::CreateNewOrg.org_url_text_field.set("www.test.com")
        #        Element::CreateNewOrg.address1_text_field.set("Keji 2 Road")
        #        Element::CreateNewOrg.address2_text_field.set("QinFengGe D201")
        #        Element::CreateNewOrg.city_ text_field.set("Xi'an")
        #        Element::CreateNewOrg.zip_text_field.set("710075")
        #        Element::CreateNewOrg.phone_text_field.set("1234567890")
        #        Element::CreateNewOrg.fax_text_field.set("0123456789")
        #        Element::CreateNewOrg.email_text_field.set("tonytest@test.com")
        #        Element::CreateNewOrg.participants_text_field.set("1000")
        #        Element::CreateNewOrg.census_pop_text_field.set("10000")
        #        Element::CreateNewOrg.no_of_members_text_field.set("1000")
        Element::CreateNewOrg.next_button.click
        sleep 1
        Element::CreateNewOrg.external_classifier_select_list.select("Non-Profit")
        Element::CreateNewOrg.org_family_select_list.select("ACC")
        Element::CreateNewOrg.family_status_select_list.select("Customer")
        Element::CreateNewOrg.market1_select_list.select("Educate")
        Element::CreateNewOrg.submarket1_select_list.select("Vocational School")
        Element::CreateNewOrg.type1_select_list.select("Aviation School")
        Element::CreateNewOrg.create_button.click

        $log.test_validate(Element::EditOrg.orgname_text_field.value.include?(@@orgname), "Org Name correct or not")
        $log.test_validate($ie.text.include?("AB"),  "State Name correct or not")
        $log.test_validate($ie.text.include?("Canada (CAD)"),  "Country Name correct or not")
        sleep 10
    end

    # Update Org's informaion
    def popup_clicker(ie,buttonname)
        hwnd = ie.enabled_popup(10)
        if(hwnd)      #yeah! a popup
            popup = WinClicker.new
            popup.makeWindowActive(hwnd) #Activate the window.
            popup.clickWindowsButton_hwnd(hwnd,buttonname) #Click the button
            popup=nil
        end
    end

    def test_update_org_information
        $log.test_notes("Start to test Update Org")
        #$ie = Watir::IE.attach(:title,"423683")
        $ie.bring_to_front
        Element::EditOrg.orgname_text_field.set(@@orgname1)
        Element::EditOrg.orgurl_text_field.set("www.google.com")
        Element::EditOrg.orgurl_verified.set
        Element::EditOrg.update_button.click_no_wait
        popup_clicker($ie,"OK")
        sleep 2
        # $ie = Watir::IE.attach(:title,@@orgname1)
        sleep 2
        $log.test_validate($ie.text.include?(@@orgname1), "Org Name correct or not")
        $log.test_validate($ie.link(:title,"http://www.test.com").exists?, "Org Url correct or not")
        #$log.test_validate($ie.text.include?("Canada (CAD)"),  "Country Name correct or not")
        sleep 10
    end

    # Add new Address-
    def test_add_new_addresses
        #$ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add New Address")
        Element::OrgHomePage.addresses_link.click
        sleep 1
        Element::Addresses.add_address_button.click
        Element::Addresses.address1_text_field.set("Tonytest")
        Element::Addresses.city_text_field.set("Xi'an")
        #bug- first select state then country would make state be blank
        Element::Addresses.country_select_list.select("United States")
        Element::Addresses.state_select_list.select("[ USA ] - Army Asia")
        Element::Addresses.zip_text_field.set("12345")
        Element::Addresses.add_button.click

        $log.test_validate($ie.text.include?("Tonytest"), "Address Name correct or not")
        $log.test_validate($ie.text.include?("Xi'an"), "City Name correct or not")
        $log.test_validate($ie.text.include?("AA"), "Region Name correct or not")
        $log.test_validate($ie.text.include?("USA"),"Country Name correct or not")
        $log.test_validate($ie.text.include?("12345"), "Zip correct or not")
        sleep 10
    end


    # Add Org Note
    def test_add_new_org_note
        #$ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add New Org Note")
        Element::OrgHomePage.org_note_history_link.click
        sleep 1
        Element::OrgNote.note_title_text_field.set("Tonytest")
        Element::OrgNote.note_details_text_field.set("Tonytestofnote")
        Element::OrgNote.add_note_button.click

        $log.test_validate($ie.text.include?("Tonytest"),"Note title correct or not")
        $log.test_validate($ie.text.include?("Tonytestofnote"),"Note detail correct or not")
        sleep 10
    end

    # Add Location for Org
    def test_add_location
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Location")
        Element::OrgHomePage.locations_link.click
        sleep 1
        Element::Locations.add_new_location_button.click
        sleep 1
        Element::Locations.location_name_text_field.set("TonytestofLocation")
        Element::Locations.location_type_select_list.select("Amphitheatre")
        Element::Locations.location_address1_text_field.set("LocationAddress3")
        Element::Locations.location_city_text_field.set("Xi'an")
        Element::Locations.location_state_select_list.select("[USA] - AA")
        Element::Locations.location_zip_text_field.set("12345")
        Element::Locations.location_phone_text_field.set("0123456789")
        Element::Locations.location_email_text_field.set("test@test.com")
        Element::Locations.location_url_text_field.set("http://www.google.com")
        Element::Locations.save_location_data_button.click

        $log.test_validate($ie.text.include?("TonytestofLocation"),"Loation Name correct or not")
        $log.test_validate($ie.text.include?("Amphitheatre"),"Location Type correct or not")
        $log.test_validate($ie.text.include?("LocationAddre... Xi'an, AA 12345"),"Location Address correct or not")
        $log.test_validate($ie.text.include?("(012) 345-6789"),"Phone number correct or not")
        $log.test_validate($ie.link(:title,"test@test.com").exists?,"Email correct or not")
        $log.test_validate($ie.link(:title,"http://www.google.com").exists?, "URL correct or not")
        sleep 10
    end
    # Add Inventory of Location to be continued


    # Create link fo Metalistings
    def test_create_link_for_metalistings
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Create link for Metalistings")
        Element::OrgHomePage.sales_link.click
        sleep 1
        Element::ContactsAndDeals.new_link_button.click
        Element::MetalistingsLinkManager::Radio_Type.new("unlinked").selection
        Element::MetalistingsLinkManager.metalisting_search_in_select_list.set("Listing Name")
        Element::MetalistingsLinkManager.metalisting_for_text_field.set("karen")
        Element::MetalistingsLinkManager.metalisting_search_button.click
        
        sleep 1
        Element::MetalistingsLinkManager.metalisting_firstrecord_checkbox.set
        id1 = Element::MetalistingsLinkManager.metalisting_firstrecord_checkbox.value
        puts id1
        $array=[]
        $array = $ie.span(:text,/Search Results/).text.split()
        Element::MetalistingsLinkManager.metalisting_lastrecord_checkbox("#{$array[3].to_i+1}").set
        id2 = Element::MetalistingsLinkManager.metalisting_lastrecord_checkbox("#{$array[3].to_i+1}").value
        puts id2
        Element::MetalistingsLinkManager.metalisting_create_link_button.click
        sleep 1

        $log.test_validate($ie.text.include?("#{id1}"), "#{id1} correct or not")
        $log.test_validate($ie.text.include?("#{id2}"),"#{id2} correct or not")
        sleep 10
    end


    # Add Promotions
    def test_add_promotions
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Promotion")
        Element::OrgHomePage.promotions_link.click
        sleep 1
        Element::Promotions.promotions_select_list.select_value("4")
        @@program_name1= Element::Promotions.promotions_select_list.option(:value,"4").text
        Element::Promotions.add_promotion_button.click
        sleep 1
        Element::Promotions.promotions_select_list.select_value("335")
        @@program_name2= Element::Promotions.promotions_select_list.option(:value,"335").text
        Element::Promotions.add_promotion_button.click
        sleep 1
        def count_promotions
            for i in 0..100
                if $ie.text_field(:name=>"cases",:index=>i+1).exist?
                    i+=1
                else
                    break
                end
            end
            return i
        end
        @@n = count_promotions
        Element::Promotions.promotion_status_select_list("1").select("Declined")
        Element::Promotions.promotion_cases_text_field("1").set("1000")
        Element::Promotions.promotion_sample_text_field("1").set("1234")
        Element::Promotions.promotion_update_button("1").click
        sleep 1
        Element::Promotions.promotion_status_select_list(@@n).select("Enrolled")
        Element::Promotions.promotion_cases_text_field(@@n).set("3000")
        Element::Promotions.promotion_sample_text_field(@@n).set("9785")
        Element::Promotions.promotion_update_button(@@n).click

        $log.test_validate($ie.text.include?("#{@@program_name1}"), "#{@@program_name1} was added or not")
        $log.test_validate($ie.text.include?("#{@@program_name2}"),"#{@@program_name2} was added or not")
        sleep 10
    end


    # Add Shipping Information
    def test_add_shipping_information
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Shipping Information")
        Element::OrgHomePage.shipping_information_link.click
        sleep 1
        #        def count_promotions
        #            for i in 0..100
        #                if $ie.text_field(:name=>"this_shipping_contact",:index=>i+1).exist?
        #                    i+=1
        #                else
        #                    break
        #                end
        #            end
        #            return i
        #        end
        #        @@n = count_promotions
        # puts @@n
        for m in 1..@@n
            Element::ShippingInformation.shipping_contact_text_field(m).set("Tony Test").each
            Element::ShippingInformation.shipping_phone_number_text_field(m).set("0123456789").each
            Element::ShippingInformation.shipping_instructions_text_field(m).set("TestConentForShippingInstructioins").each
            Element::ShippingInformation.shipping_information_update_button(m).click
            sleep 1
            $log.test_validate(Element::ShippingInformation.shipping_contact_text_field(m).value.include?("Tony Test"), "No.#{m} Shipping Contact correct or not")
            $log.test_validate(Element::ShippingInformation.shipping_phone_number_text_field(m).value.include?("012-345-6789"), "No.#{m} Shipping phone number correct or not")
            $log.test_validate($ie.text.include?("TestConentForShippingInstructioins"), "No.#{m} Shipping instruction correct or not")

        end      
        sleep 10
    end


    # Add Compliance
    def test_add_compliance
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Compliance")
        Element::OrgHomePage.compliance_link.click
        sleep 1
        #        def count_promotions
        #            for i in 0..100
        #                if $ie.button(:value=>"Update",:index=>i+1).exist?
        #                    i+=1
        #                else
        #                    break
        #                end
        #            end
        #            return i
        #        end
        #        @@n = count_promotions
        #        # puts @@n
        for m in 1..@@n
            Element::Compliance.compliance_source_select_list(m).select("Email").each
            Element::Compliance.compliance_cf_checkbox(m).set.each
            Element::Compliance.compliance_av_checkbox(m).set.each
            Element::Compliance.compliance_grade_select_list(m).select("1").each
            Element::Compliance.compliance_notes_text_field(m).set("Testnotes").each
            Element::Compliance.compliance_update_button(m).click
            sleep 1
        end
        $log.test_validate($ie.text.include?("Testnotes"), "Compliance Notes correct or not")
        $log.test_validate($ie.text.include?("Email"), "Compliance Source correct or not")
        sleep 10
    end

    #  Add Membership
    def test_add_membership
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Membership")
        Element::OrgHomePage.membership_link.click
        sleep 1
        Element::Membership.membership_add_button.click
        Element::Membership.membership_year_select_list.select("2010")
        Element::Membership.membership_source_select_list.select("Online")
        Element::Membership.membership_level_select_list.select("Silver Membership")
        Element::Membership.membership_amount_text_field.set("9999.99")
        Element::Membership.membership_pay_type_select_list.select("CC")
        sleep 1
        Element::Membership.membership_add_record_button.click
        sleep 1
        Element::Membership.org_page_button.click
        sleep 1
        $log.test_validate($ie.text.include?("2010"), "Membership Year  correct or not")
        $log.test_validate($ie.text.include?("Online"), "Membership Source correct or not")
        $log.test_validate($ie.text.include?("Silver"), "Membership Type correct or not")
        $log.test_validate($ie.text.include?("9,999.99"), "Amount correct or not")
        $log.test_validate($ie.text.include?("CC"), "Membership paid type correct or not")
        sleep 10

    end

    #  Add Special Events
    # Another bug the special events could not be delete one by one.
    def test_add_special_events
        #$ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Add Special Events")
        Element::OrgHomePage.special_events_link.click
        sleep 1
        Element::SpecialEvents.special_events_add_button.click
        Element::SpecialEvents.special_events_description_text_field.set("Tonytest_specialevents")
        Element::SpecialEvents.special_events_product_text_field.set("Tonytest_product")
        Element::SpecialEvents.special_events_qty_text_field.set("100")
        Element::SpecialEvents.special_events_cases_text_field.set("10")
        Element::SpecialEvents.special_events_notes_text_field.set("Tonytest_notes")
        Element::SpecialEvents.special_events_shipped_date_text_field.set("09/10/2009")
        Element::SpecialEvents.special_events_event_date_text_field.set("09/11/2009")
        Element::SpecialEvents.special_events_add_new_button.click
        sleep 2
        def count_special_events
            for i in 0..100
                if $ie.button(:value=>"Edit",:index=>i+1).exist?
                    i+=1
                else
                    break
                end
            end
            return i
        end
        n = count_special_events
        sleep 1
        $log.test_validate(Element::SpecialEvents.special_events_description_text_field.value.include?("Tonytest_specialevents"), "Description correct or not")
        $log.test_validate(Element::SpecialEvents.special_events_product_text_field.value.include?("Tonytest_product"), "Product correct or not")
        $log.test_validate(Element::SpecialEvents.special_events_qty_text_field.value.include?("100"), "Qty. correct or not")
        $log.test_validate(Element::SpecialEvents.special_events_cases_text_field.value.include?("10"), "Cases correct or not")
        sleep 2
        #First check the checkbox of special events then deactive org
        Element::SpecialEvents.special_events_status_checkbox(n).set
        Element::SpecialEvents.special_events_edit_button(n).click
        sleep 10
    end


    # Inactive An Org, only for the org without deal

    def test_inactive_org
        $ie = Watir::IE.attach(:title,@@orgname1)
        $log.test_notes("Start to test Inactive Org")
        Element::OrgHomePage.edit_org_button.click
        @@orgid = $ie.title
        Element::EditOrg.active_checkbox.clear
        Element::EditOrg.update_button.click_no_wait
        popup_clicker($ie,"OK")
        sleep 1
        CRM_STD_LIB.login($int,"tony2008","2165270")
        CRM_STD_LIB.open_page("Org Search")
        Element::OrgSearch.search_in_select_list.select("Org ID")
        Element::OrgSearch.for_text_field.set(@@orgid)
        Element::OrgSearch.display_status_select_list.select("Active")
        Element::OrgSearch.search_button.click
        $log.test_validate(!$ie.link(:text,@@orgid).exists?, "When display status is Active,#{@@orgid} exists or not")
        sleep 1
        CRM_STD_LIB.open_page("Org Search")
        Element::OrgSearch.search_in_select_list.select("Org ID")
        Element::OrgSearch.for_text_field.set(@@orgid)
        Element::OrgSearch.display_status_select_list.select("Inactive")
        Element::OrgSearch.search_button.click
        $log.test_validate($ie.link(:text,@@orgid).exists?, "When display status is Inctive,#{@@orgid} exists or not")
        sleep 10

    end

    # Active An Org
    def test_active_org
        $log.test_notes("Start to test active Org")
        $ie.link(:text,"#{@@orgid}").click
        sleep 5
        $ie = Watir::IE.attach(:title,@@orgid)
        Element::EditOrg.active_checkbox.set
        Element::EditOrg.update_button.click_no_wait
        popup_clicker($ie,"OK")
        sleep 1
        CRM_STD_LIB.login($int,"tony2008","2165270")
        CRM_STD_LIB.open_page("Org Search")
        sleep 5
        Element::OrgSearch.search_in_select_list.select("Org ID")
        Element::OrgSearch.for_text_field.set(@@orgid)
        Element::OrgSearch.display_status_select_list.select("Inactive")
        Element::OrgSearch.search_button.click
        $log.test_validate(!$ie.link(:text,@@orgid).exists?, "When display status is Inactive,#{@@orgid} exists or not")
        sleep 1
        CRM_STD_LIB.open_page("Org Search")
        Element::OrgSearch.search_in_select_list.select("Org ID")
        Element::OrgSearch.for_text_field.set(@@orgid)
        Element::OrgSearch.display_status_select_list.select("Active")
        Element::OrgSearch.search_button.click
        $log.test_validate($ie.link(:text,@@orgid).exists?, "When display status is Active,#{@@orgid} exists or not")
        $ie.close
    end

    # Search An Org
    #def test_search_org

    #end



    def test_reset

    end

    def test_tear_down
        @@for = nil
        @@search_in = nil
    end


    class NewOrg < Watir::TestCase
        def test_setup
        end

        def test_tear_down
            $log.close unless $log.nil?
        end
    end

end


